package cn.innosmart.aq.customize.datetimepicker;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.CheckBox;

import cn.innosmart.aq.R;

public class CheckBoxWithCircularIndicator extends CheckBox {

    private final int mCircleColor;
    private final String mItemIsSelectedText;
    private final int mRadius;
    private Paint mCirclePaint = new Paint();
    private boolean mDrawCircle;

    public CheckBoxWithCircularIndicator(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        Resources res = context.getResources();
        mCircleColor = res.getColor(R.color.font_lightwhite);
        mRadius = res.getDimensionPixelOffset(com.fourmob.datetimepicker.R.dimen.month_select_circle_radius);
        mItemIsSelectedText = context.getResources().getString(com.fourmob.datetimepicker.R.string.item_is_selected);
        setButtonDrawable(null);
        init();
    }

    private void init() {
        mCirclePaint.setFakeBoldText(true);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setTextAlign(Paint.Align.CENTER);
        mCirclePaint.setStyle(Paint.Style.FILL);
        mCirclePaint.setAlpha(60);
    }

    @Override
    public void setChecked(boolean checked) {
        mDrawCircle = checked;
        super.setChecked(checked);
    }


    public CharSequence getContentDescription() {
        CharSequence text = getText();
        if (mDrawCircle) {
            text = String.format(mItemIsSelectedText, text);
        }
        return text;
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mDrawCircle) {
            int width = getWidth();
            int heigth = getHeight();
            int radius = Math.min(width, heigth) / 2;
            canvas.drawCircle(width / 2, heigth / 2, radius, mCirclePaint);
        }
    }
}